package com.xw.bo.ks.util;

import com.xw.bo.ks.entity.enums.ExceptionEnum;
import com.xw.bo.ks.entity.exception.BoException;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;

import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author： XW -> ~
 * @create： 2024/1/5 11:34
 */
public class UriUtil {

    public static Map<String, NameValuePair> getNamePairMap(String url) {

        Map<String, NameValuePair> pairMap = new HashMap<>();
        try {
            // 创建URI对象
            URI uri = new URI(url);
            // 使用URLEncodedUtils解析参数
            List<NameValuePair> queryParams = URLEncodedUtils.parse(uri, StandardCharsets.UTF_8);
            pairMap = BoCollectionUtil.getMapToList(queryParams, NameValuePair::getName);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }

        return pairMap;

    }

    public static void main(String[] args) {

        Map<String, NameValuePair> namePairMap = getNamePairMap("https://cps.kwaixiaodian.com/zone/cps/investment/activity-detail?activityId=6251439793&showSign=true");
        if (namePairMap.containsKey("activityId")) {
            NameValuePair nameValuePair = namePairMap.get("activityId");
            try {

                System.out.println("activityId = " + Long.parseLong(nameValuePair.getValue()));
            } catch (Exception e) {
                throw new BoException(ExceptionEnum.ACTIVITY_ID_PARSE_ERROR);
            }
        } else {
            throw new BoException(ExceptionEnum.ACTIVITY_ID_PARSE_ERROR);
        }

    }
}
